#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        queue<TreeNode*> as;
        vector<vector<int>> ret;
        if (root == nullptr)
        {
            return ret;
        }
        as.push(root);
        int h = 0;
        while (!as.empty())
        {
            int i = as.size();
            vector<int> aq;
            for (int j = 0; j < i; j++)
            {
                auto w = as.front();
                as.pop();
                aq.push_back(w->val);

                if (w->left)
                {
                    as.push(w->left);
                }
                if (w->right)
                {
                    as.push(w->right);
                }
            }
            if (h % 2 != 0)
            {
                reverse(aq.begin(), aq.end());
            }
            h++;
            ret.push_back(aq);
        }
        return ret;
    }
};